package cn.xizikelif.basic.util;

import cn.xizikelif.basic.cofig.SpringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class MailUtil {


    public static void getMailUtil(String setFrom, String text) {
        ApplicationContext contex = SpringUtils.getContex();
        JavaMailSender javaMailSender = contex.getBean(JavaMailSender.class);
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("408788353@qq.com");
            helper.setSubject(setFrom);
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText(text);
          /*  // 4.添加附件
            helper.addAttachment("好东西.jpg", new File("C:\\Users\\西兹克利夫\\Downloads\\1162e8ec2495cb2a268bce386f4782ce.jpeg"));*/
            // 5.设置收件人
            helper.setTo("408788353@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
